import path, {resolve} from 'path'
import { defineConfig,loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import styleImport, { VantResolve } from 'vite-plugin-style-import';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import { viteMockServe } from "vite-plugin-mock";



// https://vitejs.dev/config/
const viteProject = ({ mode }) => {
	process.env = { ...process.env, ...loadEnv(mode, process.cwd()) };
	const localEnabled: boolean = (process.env.VITE_MOCK as unknown as boolean) || false;
	const prodEnabled: boolean = (process.env.USE_CHUNK_MOCK as unknown as boolean) || false;

  return defineConfig({
		resolve: {
			alias:{
				//别名设置
				'@': resolve(__dirname,'src'),
				'@viwes':resolve(__dirname,"src/viwes"),
				'@comp': resolve(__dirname,"src/components"),
				'@utils': resolve(__dirname,"src/utils"),
				"@api":resolve(__dirname,"src/api")
			}
		},
		plugins: [
			vue(),
			createSvgIconsPlugin({
				iconDirs: [path.resolve(process.cwd(), 'src/icons/svg')],
				symbolId: 'icon-[dir]-[name]',
				inject: 'body-last',
				customDomId: '__svg__icons__dom__'
			}),
			styleImport({
				resolves: [VantResolve()],
			}),
			viteMockServe({
        mockPath: './mock/data',
        localEnabled: localEnabled,
        // 实际开发关闭
        prodEnabled: prodEnabled,
        // injectCode: `
        //     import { setupProdMockServer } from './mock/index';
        //     setupProdMockServer();
        // `,
      })
		],
    base: process.env.VITE_BASEURL,
		 // 打包配置
		build: {
      sourcemap: true,
      outDir: 'dist', //指定输出路径
      assetsDir: 'static/img/', // 指定生成静态资源的存放路径
    },
		server: {
			host:"localhost",
			port: 3000,
			proxy: {
				// '/viteApp/api/mock/': {
				// 	target: 'http://localhost:3002/', //对mock进行代理，为了区别非mock的代理
				// 	changeOrigin: true,
				// 	//rewrite: (path) => path.replace(/^\/mock/, '')
				// },
				'/japi': {
					target: 'http://localhost:3000/', 
					changeOrigin: true,
					rewrite: (path) => path.replace(/^\/japi/, '')
				},
			},
			open: true,
		}
  })
}
export default viteProject;